package conMgr.ui;

import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import conMgr.model.Conference;
import conMgr.model.User;
import conMgr.model.UserType;

import conMgr.ui.util.SelectUserView;
import conMgr.ui.util.Ui;


public class AssignProgramChair extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6844966544882980288L;
	
	private Conference newConference;
	
	/**
	 * The text box that will display the currently selected user.
	 */
	private final JTextField textBox;
	
	/**
	 * The selected program chair.
	 */
	private User user;

	/**
	 * Initializes a new AssignSubprogramChairView.
	 * @param paper The paper that will have a subprogram chair assigned.
	 */
	public AssignProgramChair()
	{
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = Ui.DEFAULT_INSETS;
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.anchor = GridBagConstraints.WEST;
		add(new JLabel("Select Program Chair:"), constraints);
		
		constraints = new GridBagConstraints();
		constraints.insets = Ui.DEFAULT_INSETS;
		constraints.gridx = 1;
		constraints.gridy = 0;
		constraints.anchor = GridBagConstraints.WEST;
		textBox = new JTextField((user != null ? user.getName() : ""), 15);
		add(textBox, constraints);
		
		constraints = new GridBagConstraints();
		constraints.insets = Ui.DEFAULT_INSETS;
		constraints.gridx = 2;
		constraints.gridy = 0;
		constraints.anchor = GridBagConstraints.WEST;
		add(Ui.createButton("...", new PickUserAction()), constraints);
	}
	
	/**
	 * Saves the newly selected subprogram chair to the paper.
	 */
	public void save()
	{
		newConference.setProgramChair(user);
	}
	
	/**
	 * Provides an action for the "..." button. 
	 */
	class PickUserAction extends AbstractAction
	{
		/**
		 * The serialization ID of this class.
		 */
		private static final long serialVersionUID = 7536822585168351327L;

		/**
		 * Handles the action. Shows the SelectUserView.
		 */
		@Override
		public void actionPerformed(final ActionEvent arg0)
		{
			final User user = SelectUserView.getUser(UserType.PROGRAM_CHAIR);
			
			if (user == null)
				return;
			
			AssignProgramChair.this.textBox.setText(user.getName());
			AssignProgramChair.this.user = user;
		}
	}
}
